
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Simcha Michal
 */
public class Roots {

    private ArrayList<String> roots = new ArrayList();
    // The patern I am using to find the shoresh."[ותמהשנאכי]...[ותמהשנאכי]"
  
    
 
    public String getRoot(String word) {
        if (checkWhetherAlredyHaveTheRoot(word)) {
            return findTheRootsInTheRootsList(word);
        } else {
            return findingRoot(word);
        }
    }



    

    private String findingRoot(String word) {
        if (word.length() == 3 || word.length() == 2) {
            roots.add(word.trim());
            System.out.println(word.trim());
            return word.trim();
        } else {
            ArrayList<String> prefixesToBeReplaced = new ArrayList<>();
            prefixesToBeReplaced.add("^[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]");
            prefixesToBeReplaced.add("^[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]");
            prefixesToBeReplaced.add("^[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]");
            prefixesToBeReplaced.add("^[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]");
            prefixesToBeReplaced.add("^[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]");
            prefixesToBeReplaced.add("^[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]");
            prefixesToBeReplaced.add("^[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]");
            prefixesToBeReplaced.add("^[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]");
            prefixesToBeReplaced.add("^[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]");
            ArrayList<String> sufixesToBeReplaced = new ArrayList<>();
            sufixesToBeReplaced.add("[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]$");
            sufixesToBeReplaced.add("[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]$");
            sufixesToBeReplaced.add("[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]$");
            sufixesToBeReplaced.add("[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]$");
            sufixesToBeReplaced.add("[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]$");
            sufixesToBeReplaced.add("[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]$");
            sufixesToBeReplaced.add("[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]$");
            sufixesToBeReplaced.add("[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]$");
            sufixesToBeReplaced.add("[\u05D0\u05D1\u05DB\u05D9\u05DE\u05EA\u05D4\u05E0\u05D5]$");
            String root = null;
            boolean st = true;
            boolean lessThanThree = true;
            Iterator ip=prefixesToBeReplaced.listIterator();
            Iterator is=sufixesToBeReplaced.listIterator();
            for (;ip.hasNext()&&is.hasNext();) {
                String regex=ip.next().toString()+is.next().toString();
                if (st) {
                    root = word.replaceAll(regex, "").trim();
                    st = false;
                } else if (lessThanThree) {
                    root = root.replaceAll(regex, "").trim();

                    if (root.length() == 3) {
                        lessThanThree = false;
                    }
                }
            }
             roots.add(root.trim());
            System.out.println(root.trim());
            return root.trim();
          
           
        }

    }

    private boolean checkWhetherAlredyHaveTheRoot(String w) {
        if (!roots.isEmpty()) {
            for (String r : roots) {
                Pattern p = Pattern.compile(w);
                Matcher m = p.matcher(r);
                if (m.matches()) {
                    return true;
                }
            }
        }
        return false;
    }

    private String findTheRootsInTheRootsList(String word) {
        for (String w : roots) {
            Pattern p = Pattern.compile(word);
            Matcher m = p.matcher(w);
            if (m.matches()) {
                return w;
            }
        }
        return null;
    }
}
